package com.mall.inventory.server.service.impl;

import com.mall.common.core.domain.Result;
import com.mall.inventory.common.dto.CmsPurchaseOrderDto;
import com.mall.inventory.common.dto.CmsPurchasePlanDto;
import com.mall.inventory.common.po.CmsPurchaseOrder;
import com.mall.inventory.common.po.CmsPurchasePlan;
import com.mall.inventory.common.vo.CmsSkuInfoVo;
import com.mall.inventory.server.mapper.InventoryMapper;
import com.mall.inventory.server.service.InventoryService;
import com.mall.product.common.domain.PmsSkuInfo;
import com.mall.product.remote.feign.PmsSkuInfoRemote;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

import static com.mall.common.core.utils.PageUtils.startPage;

@Service
public class InventoryServiceImpl implements InventoryService{

    @Autowired
    private InventoryMapper inventoryMapper;

    @Autowired
    private PmsSkuInfoRemote pmsSkuInfoRemote;
    /**
     * 商家端：
     * 添加采购计划
     * @param skuIds
     * @return
     */
    @Override
    public Result addCmsPurchasePlan(String skuIds) {
        if(skuIds==null || skuIds.equals("")){
            return Result.error("请选中数据后进行添加!");
        }
        String[] skuIdList = skuIds.split(",");
        //获取店铺ID
        //Long shopId = SecurityUtils.getLoginUser().getSysUser().getShopId();
        Long shopId = 1L;
        //进行批量添加
        int i = inventoryMapper.addCmsPurchasePlan(skuIdList,shopId);
        if(i>0){
            return Result.success(null,"添加成功");
        }
        return Result.error("请稍后重试!");
    }

    /**
     * 商家端：
     * 添加采购订单
     * @param cmsPurchaseOrderDto
     * @return
     * @throws RuntimeException
     */
    @Transactional
    @Override
    public Result addPurchaseOrder(CmsPurchaseOrderDto cmsPurchaseOrderDto) throws RuntimeException{
        //获取店铺ID
        //Long shopId = SecurityUtils.getLoginUser().getSysUser().getShopId();
        Long shopId = 1L;
        //获取采购计划ids
        String planIds = cmsPurchaseOrderDto.getPlanIds();
        //将string转换成集合
        String[] planIdList = planIds.split(",");
        //查询采购计划
        List<CmsPurchasePlan> planList = inventoryMapper.findByPlanids(planIdList);
        //判断选择的采购计划状态是否为代采购
        for (CmsPurchasePlan cmsPurchasePlan : planList) {
            if(cmsPurchasePlan.getPlanStatus()==0){
                return Result.error("请先审核你选择的采购计划!");
            }
            if(cmsPurchasePlan.getPlanStatus()==2){
                return Result.error("采购计划ID："+cmsPurchasePlan.getSkuId()+"..."+"已提交采购单!");
            }
        }
        //批量添加采购计划详情
        CmsPurchaseOrder cmsPurchaseOrder = new CmsPurchaseOrder();
        cmsPurchaseOrder.setSupplierId(cmsPurchaseOrderDto.getSupplierId());
//        cmsPurchaseOrder.setUserId(SecurityUtils.getUserId());
        cmsPurchaseOrder.setUserId(1L);
        //添加采购单
        int i = inventoryMapper.addPurchaseOrder(cmsPurchaseOrder);

        //添加采购单详情表
        int j = inventoryMapper.addPurchaseOrderDetail(cmsPurchaseOrder.getOrderId(), planIdList,shopId);

        //修改采购计划状态
        for (CmsPurchasePlan cmsPurchasePlan : planList) {
            cmsPurchasePlan.setPlanStatus(2);
            inventoryMapper.updatePlan(cmsPurchasePlan);
        }

        if(i+j<2){
            //抛出异常
            throw new RuntimeException("添加失败");
        }
        return Result.success("生成采购单成功");
    }

    /**
     * 商家端：
     * 通过登录人信息查询所属店铺采购计划列表
     * @param planStatus
     * @return
     */
    @Override
    public Result CmsPurchasePlanList(Integer planStatus) {
        startPage();
        //1.获取用户信息
        //Long shopId = SecurityUtils.getLoginUser().getSysUser().getShopId();
        Long shopId = 1L;
        //2.通过店铺ID 查询采购计划列表
        List<CmsPurchasePlan> cmsPurchasePlanList = inventoryMapper.findByShopId(shopId,planStatus);

        //3.返回用户优惠券列表
        return Result.success(cmsPurchasePlanList);
    }

    @Override
    public Result getSkuInfoList() {
        //获取用户信息
        Long shopId = 1L;
        //通过用户信息查询此店铺下的低库存sku信息
        List<PmsSkuInfo> byShopId = pmsSkuInfoRemote.findLowInventoryNumSkuList(shopId).getData();
        System.out.println(byShopId);
        ArrayList<CmsSkuInfoVo> cmsSkuInfos = new ArrayList<>();
        for (PmsSkuInfo pmsSkuInfo : byShopId) {
            CmsSkuInfoVo cmsSkuInfo = new CmsSkuInfoVo();
            cmsSkuInfo.setCommodityId(pmsSkuInfo.getSkuId());
            cmsSkuInfo.setCommodityName(pmsSkuInfo.getSkuName());
            cmsSkuInfo.setCommodityPic(pmsSkuInfo.getSkuDefaultImg());
            cmsSkuInfo.setCommodityInventory(pmsSkuInfo.getInventoryNum());
            cmsSkuInfos.add(cmsSkuInfo);
        }
        return Result.success(cmsSkuInfos);
    }

    @Override
    public Result updatePlan(CmsPurchasePlan cmsPurchasePlan) {
        if(cmsPurchasePlan.getPlanNum()==null){
            return Result.error("请输入采购数量");
        }
        return Result.success(inventoryMapper.updatePlan(cmsPurchasePlan));
    }
}
